using System;
using System.Collections.Generic;
namespace HKB.Pay
{
	public class PayProvider
	{
		private static APayProvider _instance = null;
		private static object lockHelper = new object();
		public static List<APayProvider> _array = new List<APayProvider>();
		private PayProvider()
		{
		}
		private static void GetProvider(string PayTypeName)
		{
			try
			{
				string typeName = string.Format("HKB.Pay.{0}.PayProvider,HKB.Pay.{0},Version=1.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089", PayTypeName);
				Type type = Type.GetType(typeName);
				PayProvider._instance = (APayProvider)Activator.CreateInstance(Type.GetType(typeName, false, true));
				PayProvider._instance.PayTypeName = PayTypeName;
				PayProvider.SaveInstance(PayProvider._instance, PayTypeName);
			}
			catch
			{
				throw new Exception("请检查PayTypeName类型是否正确，例如：AliPay、Trafuton、ChinaBank");
			}
		}
		public static APayProvider GetInstance(string PayTypeName)
		{
			APayProvider result;
			if (PayProvider._instance == null)
			{
				lock (PayProvider.lockHelper)
				{
					if (PayProvider._instance == null)
					{
						PayProvider.GetProvider(PayTypeName);
					}
				}
			}
			else
			{
				for (int i = 0; i < PayProvider._array.Count; i++)
				{
					if (PayProvider._array[i].PayTypeName == PayTypeName)
					{
						result = PayProvider._array[i];
						return result;
					}
				}
				PayProvider.GetProvider(PayTypeName);
			}
			result = PayProvider._instance;
			return result;
		}
		private static void SaveInstance(APayProvider instance, string PayTypeName)
		{
			for (int i = 0; i < PayProvider._array.Count; i++)
			{
				if (PayProvider._array[i].PayTypeName == PayTypeName)
				{
					return;
				}
			}
			PayProvider._array.Add(instance);
		}
		public static void ResetDbProvider()
		{
			PayProvider._instance = null;
		}
	}
}
